package cn.roadtrip.mtravelbusinessmodule.bs.travelnote;

import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelnote.tables.CoupleNoteLike;
import cn.roadtrip.utilmodule.util.DataBean;
import com.github.yitter.idgen.YitIdHelper;
import lombok.*;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

import static cn.roadtrip.mtravelbusinessmodule.sql.couple.travelnote.tables.CoupleNoteLike.COUPLE_NOTE_LIKE;

@Component
public class TravelNoteImpl {
    @Autowired
    private DSLContext dslContext;

    /**
     *
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode
    public static class Model_createTravelNote {
        private String userId;
        private String title;
        private java.util.Map<String, Object> conent;
        private java.util.List<String> image;
        private Integer sortNum;
        private java.util.List<String> tags;
        /**
         * title
         * zan_num
         * user_id
         * comment_num
         * content
         * img
         * video
         * share_num
         * browse_num
         * tenant_id
         * sort
         * tags
         */
    }

    public DataBean createTravelNote() {
        return null;
    }

    /**
     *
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode
    public static class Model_doTravelNoteLike {
        private String userId;
        private String travelNoteId;
    }

    /**
     * 赞操作
     *
     * @param model
     * @return
     */
    public DataBean doTravelNoteLike(Model_doTravelNoteLike model) {
        return dslContext.transactionResult(ctx -> {
            int count = dslContext.selectOne()
                    .from(
                            COUPLE_NOTE_LIKE
                    ).where(
                            COUPLE_NOTE_LIKE.USERID.eq(model.getUserId())
                    ).and(
                            COUPLE_NOTE_LIKE.NOTEID.eq(model.getTravelNoteId())
                    ).limit(1)
                    .fetchOne().value1();
            if (count > 0) {
                //删除
                dslContext.deleteFrom(
                        COUPLE_NOTE_LIKE).where(
                        COUPLE_NOTE_LIKE.USERID.eq(model.getUserId())
                ).and(
                        COUPLE_NOTE_LIKE.NOTEID.eq(model.getTravelNoteId())
                ).execute();
            } else {
                //新增
                dslContext.insertInto(
                                COUPLE_NOTE_LIKE
                        ).set(
                                COUPLE_NOTE_LIKE.USERID, model.getUserId()
                        ).set(
                                COUPLE_NOTE_LIKE.NOTEID, model.getTravelNoteId()
                        ).set(
                                COUPLE_NOTE_LIKE.ID, "" + YitIdHelper.nextId()
                        ).set(
                                COUPLE_NOTE_LIKE.CREATETIME, System.currentTimeMillis()
                        )
                        .execute();
            }
            dslContext.commit();
            return DataBean.suc(true, 1, "suc", "操作成功");
        });
    }

    /**
     *
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode
    private static class Model_countTravelNoteLikeNum {
        private java.util.List<String> travelNoteIds;
    }

    /**
     *
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @EqualsAndHashCode
    private static class VPO_countTravelNoteLikeNum {
        private String noteId;
        private Integer num;
    }

    /**
     * 获取游记点赞数
     *
     * @return
     */
    public DataBean countTravelNoteLikeNum(Model_countTravelNoteLikeNum model) {
        return dslContext.transactionResult(ctx -> {
            try {
                java.util.List<VPO_countTravelNoteLikeNum> ls = dslContext.select(
                        DSL.field("t.noteId"),
                        DSL.count(DSL.field("t.userId")).as("num")
                ).from(
                        DSL.select(
                                COUPLE_NOTE_LIKE.NOTEID.as("noteId"),
                                COUPLE_NOTE_LIKE.USERID.as("userId")
                        ).from(
                                COUPLE_NOTE_LIKE
                        ).where(
                                COUPLE_NOTE_LIKE.NOTEID.in(model.getTravelNoteIds())
                        ).asTable("t")
                ).groupBy(
                        DSL.field("t.noteId")
                ).fetchInto(VPO_countTravelNoteLikeNum.class);
                java.util.Map<String, Integer> map = ls.stream().collect(Collectors.toMap(VPO_countTravelNoteLikeNum::getNoteId, VPO_countTravelNoteLikeNum::getNum));
                return DataBean.suc(map, map.size(), "suc", "操作成功");
            } catch (Exception e) {
                e.printStackTrace();
                return DataBean.sucEmpty();
            }
        });
    }

}
